<template>
  <div class="task-box">
    <div style="position: relative; min-height: 60px">
      <TitleName
        class="title absolute z-1 top-0 left-0"
        icon="i-carbon-bookmark-filled"
        title="任务记录"
      ></TitleName>
      <!-- 查询条件 -->
      <div
        style="
          display: flex;
          position: absolute;
          right: 0px;
          margin-left: 100px;
        "
      >
        <el-form id="queryForm" :inline="true" :model="queryForm">
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyword"
              clearable
              placeholder="请输入关键字"
              style="width: 180px"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="dateRange"
              end-placeholder="结束日期"
              range-separator="至"
              start-placeholder="开始日期"
              style="width: 225px"
              type="daterange"
              value-format="YYYY-MM-DD"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              clearable
              placeholder="任务状态"
              style="width: 100px"
            >
              <el-option
                v-for="item in rsgl_task_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button plain type="primary" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addItem">分配任务</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div v-if="total > 0" style="display: block; width: 100%">
      <div
        v-for="item in taskList"
        :key="item.id"
        :label="item.id"
        :value="item.id"
        class="task-flex"
      >
        <div class="task-flex-6">
          <div>
            <el-icon :size="14" style="float: left; margin: 8px 4px 0 0"
              ><Clock />
            </el-icon>
            开始时间
          </div>
          <div class="task-title">{{ item.startTime }}</div>
        </div>
        <div class="task-flex-6">
          <div>任务标题</div>
          <div class="task-title">{{ item.title }}</div>
        </div>
        <div class="task-flex-3">
          <div>负责人员</div>
          <div class="task-title">
            <el-avatar
              :size="20"
              :src="circleUrl"
              style="float: left; margin: 5px 6px 0 0"
            />
            {{ item.director }}
          </div>
        </div>
        <div class="task-flex-3">
          <div>任务状态</div>
          <div class="task-title">
            <span
              :class="
                item.status == '1' || item.status == '0'
                  ? 'orange-color'
                  : item.status == '5'
                  ? 'green-color'
                  : item.status == '3'
                  ? 'red-color'
                  : item.status == '2' || item.status == '4'
                  ? 'blue-color'
                  : ''
              "
              style="font-family: '微软雅黑', sans-serif; margin-right: 5px"
              >●</span
            >
            {{
              rsgl_task_status.find(val => val.value == item.status)
                ? rsgl_task_status.find(val => val.value == item.status).label
                : '--'
            }}
          </div>
        </div>
        <div class="task-flex-3">
          <div>紧要程度</div>
          <div class="task-title">
            {{
              rsgl_urgent_status.find(val => val.value == item.impSign)
                ? rsgl_urgent_status.find(val => val.value == item.impSign)
                    .label
                : '--'
            }}
          </div>
        </div>
        <div class="task-flex-2">
          <div>
            <el-button
              class="task-bottom"
              icon="CircleCheck"
              size="small"
              @click="taskItem(item)"
            >
              详情
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <div
      v-else
      style="
        font-size: 16px;
        font-weight: 600;
        margin-top: 50px;
        text-align: center;
      "
    >
      <p><img src="@/assets/images/icon/empty.png" style="width: 100px" /></p>
      <p>暂无数据</p>
    </div>
  </div>
  <pagination
    v-show="total > 0"
    v-model:limit="queryForm.pageSize"
    v-model:page="queryForm.pageNum"
    :total="total"
    @pagination="getList"
  />
  <!-- 新建任务 -->
  <TaskFrom
    :dialogParam="taskForm"
    @closeDialog="closeFn"
    @refreshList="refreshList"
  ></TaskFrom>
  <!-- 任务详情 -->
  <TaskDetailFrom
    :dialogParam="taskDetailParam"
    @close="closeFn"
  ></TaskDetailFrom>
</template>
<script name="taskDetails" setup>
import headaImage from '@/assets/images/icon/principal.png'
import { getTaskBusinessList } from '@/api/taskManage/task' //任务详细
import { reactive, getCurrentInstance, toRefs } from 'vue'
import TaskFrom from '@/views/rsgl/taskManage/modules/add.vue'
import TaskDetailFrom from '@/views/rsgl/taskManage/modules/details.vue'
import { useRouter } from 'vue-router'
const { proxy } = getCurrentInstance()
const props = defineProps({
  taskParam: {
    type: Object
  }
})
const router = useRouter()
const route = useRoute()
const { rsgl_urgent_status, rsgl_task_status } = proxy.useDict(
  'rsgl_urgent_status',
  'rsgl_task_status'
)
const data = reactive({
  taskDetailParam: {}, //任务详情
  taskForm: {},
  circleUrl: headaImage,
  // 日期范围
  dateRange: [],
  taskList: [], //任务详细
  total: 0,
  // 显示搜索条件
  queryForm: {
    keyword: null,
    status: null,
    pageNum: 1,
    pageSize: 10
  }
})
const {
  queryForm,
  taskList,
  circleUrl,
  dateRange,
  taskForm,
  taskDetailParam,
  total
} = toRefs(data)

async function getList() {
  let param = JSON.parse(JSON.stringify(queryForm.value))
  param.startTime = dateRange.value != null ? dateRange.value[0] : ''
  param.endTime = dateRange.value != null ? dateRange.value[1] : ''
  await getTaskBusinessList(param).then(response => {
    if (response.code == 200) {
      taskList.value = response.rows
      total.value = response.total
    }
  })
}
async function init() {
  queryForm.value = { ...queryForm.value, ...props.taskParam }
  console.log('queryForm.value', queryForm.value)
  await getList()
}
// init()

onMounted(() => {
  if (JSON.stringify(props.taskParam) != '{}') {
    console.log('props.taskParam', props.taskParam)
    init()
  } else {
    init()
  }
})

function taskItem(row) {
  console.log('row==', row)

  router.push({
    path: '/rsgl/other/taskDetails',
    query: { id: row.id }
  })

  //   taskDetailParam.value.isVisible = true
  //   taskDetailParam.value.id = rowId
}

/** 搜索按钮操作 */
function handleQuery() {
  queryForm.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  queryForm.value = {
    pageNum: 1,
    pageSize: 10,
    businessType: props.taskParam.businessType,
    businessId: props.taskParam.businessId
  }
  dateRange.value = []
  getList()
}
//添加任务
function addItem() {
  //   taskForm.value = props.taskParam
  //   taskForm.value.isVisible = true
  //   console.log('taskForm', taskForm.value)
  let object = {
    businessType: props.taskParam.businessType,
    businessId: props.taskParam.businessId,
    businessName: props.taskParam.businessName
  }
  router.push({
    path: '/rsgl/other/addTasking',
    query: { taskParam: JSON.stringify(object) }
  })
}
//关闭
const closeFn = row => {
  switch (row) {
    case 'task':
      // 新建任务
      taskForm.value.isVisible = false
      break
    case 'taskDeatil':
      // 任务详情
      taskDetailParam.value.isVisible = false

      break
  }
}
function refreshList() {
  console.log('新增')
  getList()
}
</script>
<style lang="scss" scoped>
:deep(.el-form--inline .el-form-item) {
  margin-right: 5px;
}
:deep(.el-form-item--default) {
  margin-bottom: 10px;
}
.task-box {
  padding: 0px 10px 10px 10px;
  .orange-color {
    color: #f59a23;
  }
  .blue-color {
    color: #409efc;
  }
  .green-color {
    color: #4bd863;
  }
  .red-color {
    color: red;
  }
  .task-flex {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    border: 1px solid #eee;
    padding: 12px 18px;
    line-height: 40px;
    margin-top: 18px;

    .task-flex-6 {
      flex: 5;
      font-size: 12px;
      color: #999;
    }
    .task-flex-6 div {
      line-height: 30px;
      display: block;
    }
    .task-flex-3 {
      flex: 3;
      font-size: 12px;
      color: #999;
    }
    .task-flex-3 div {
      line-height: 30px;
      display: block;
    }
    .task-flex-2 {
      flex: 2;
      font-size: 12px;
      color: #999;
      margin-top: 10px;
    }
    .task-title {
      font-size: 14px;
      color: #222;
    }
    .task-bottom {
    }
  }
}
</style>
